<template>
  <div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button color="#FFD700" block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <div class="box">
      <div></div>
      <div @click="fn">登录</div>
    </div>
  </div>
</template>

<script>
import { register } from "../api/user";

export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    fn() {
      this.$router.push("/login");
    },
    async onSubmit() {
      this.$toast.loading({
        message: "登录中...",
        forbidClick: true,
      });
      try {
        await register({
          username: this.username,
          password: this.password,
        });

        this.$router.push("/login");
      } catch (error) {
        this.$toast.fail("注册失败");
      }
    },
  },
};
</script>

<style>
.box {
  display: flex;
  justify-content: space-between;
}
</style>
